package org.reliablesource.prism.ui.workbench.actions;

/**
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Mar 15, 2006
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUpdateEvent;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;
import org.reliablesource.prism.ui.util.PrismUIConstants;

public class LoadImagesAction extends Action implements IWorkbenchWindowActionDelegate {
	IWorkbenchWindow window;
	List changeList;
	
	public LoadImagesAction(){
		this.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER));		
	}
	
	
	public void dispose() {
	}

	public void init(IWorkbenchWindow _win) {
		window = _win;
		changeList = new ArrayList<IPrismElement>();
	}

	public void run(IAction _action) {
		FileDialog dlg = new FileDialog(Display.getDefault().getActiveShell(), SWT.OPEN);
		String [] ext = {"*.xml"};
		dlg.setFilterExtensions(ext);
		String chosenPath = dlg.open();
		if(chosenPath == null){
			// End user canceled
			return;
		}
		
		File path = new File(chosenPath);
		System.err.println("Path chosen:\t" + chosenPath);
		if(!path.exists()){
			FileNotFoundException _fnfe= new FileNotFoundException(chosenPath);
			IStatus _status = StatusUtil.newStatus(Status.ERROR, "File doesn't exist", _fnfe);
			ErrorDialog.openError(window.getShell(), "Error loading resource", StatusUtil.getCause(_fnfe).getLocalizedMessage(), _status);
			return;
		} else if(!path.isFile()){
			FileNotFoundException _fnfe= new FileNotFoundException(chosenPath);
			IStatus _status = StatusUtil.newStatus(Status.ERROR, "Invalid resource type chosen: " + chosenPath + " is not a file.", _fnfe);
			ErrorDialog.openError(window.getShell(), "Error loading resource", StatusUtil.getCause(_fnfe).getLocalizedMessage(), _status);
			return;
		}
				IPrismTutorial tut = null;
				
				try {
					tut = PrismCorePlugin.getDefault().loadTutorial(chosenPath);
					PrismCorePlugin.getDefault().getModel().addChild(tut);
					
					IPrismUpdateEvent _event = PrismCorePlugin.getDefault().getElementFactory().createUpdateEvent(IPrismUpdateEvent.ADD);
					_event.addToChangedList(tut);
					
					changeList.add(PrismUIConstants.ADD_TUTORIAL_ELT);
					changeList.add(_event);
					((Observable)PrismCorePlugin.getDefault().getModel()).notifyObservers(_event);					
				} catch(ClassNotFoundException _cnfe){
					IStatus _status = StatusUtil.newStatus(Status.ERROR, _cnfe.getLocalizedMessage(), _cnfe);
					ErrorDialog.openError(window.getShell(), "Error loading resource", StatusUtil.getCause(_cnfe).getLocalizedMessage(), _status);
				}catch(FileNotFoundException _fnfe){
					IStatus _status = StatusUtil.newStatus(Status.ERROR, _fnfe.getLocalizedMessage(), _fnfe);
					ErrorDialog.openError(window.getShell(), "Error loading resource", StatusUtil.getCause(_fnfe).getLocalizedMessage(), _status);
				}catch (IOException e) {
					IStatus _status = StatusUtil.newStatus(Status.ERROR, e.getLocalizedMessage(), e);
					ErrorDialog.openError(window.getShell(), "Error loading resource", StatusUtil.getCause(e).getLocalizedMessage(), _status);
				} 

//				window.getActivePage().showView(NavigatorViewPart.VIEW_ID);
//				window.getActivePage().showView(AnimationViewPart.VIEW_ID);
	}

	public void selectionChanged(IAction action, ISelection selection) {		
	}

}
